/**
 * Created by zhLEi on 2016/4/25.
 */
angular.module('cmsysApp').service('analysisService', ['$http', 'baseService', function ($http, baseService) {
        var self = this;
        this.options = {
            barOption: {
                title: {
                    text: '各行业合同量统计',
                    x: 'center'
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                        type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                    }
                },
                toolbox: {
                    feature: {
                        dataView: {readOnly: false},
                        restore: {},
                        saveAsImage: {}
                    }
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: [{
                    type: 'category',
                    data: []
                }],
                yAxis: [{
                    type: 'value'
                }],
                series: [{
                    name: '合同量',
                    type: 'bar',
                    data: []
                }]
            },
            pieOption: {
                title: {
                    text: '各行业合同量统计',
                    x: 'center'
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                toolbox: {
                    feature: {
                        dataView: {readOnly: false},
                        restore: {},
                        saveAsImage: {}
                    }
                },
                legend: {
                    orient: 'vertical',
                    left: 'left',
                    data: []
                },
                series: [
                    {
                        name: '各行业',
                        type: 'pie',
                        radius: '65%',
                        center: ['52%', '60%'],
                        data: [],
                        itemStyle: {
                            emphasis: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            },
            mapOption: {
                title: {
                    text: '各省份合同数及总金额统计',
                    left: 'center'
                },
                tooltip: {
                    trigger: 'item',
                    //formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    orient: 'vertical',
                    left: 'left',
                    data: ['合同数', '总金额']
                },
                visualMap: {
                    min: 0,
                    max: 2500,
                    left: 'left',
                    top: 'bottom',
                    text: ['高', '低'],           // 文本，默认为数值文本
                    calculable: true
                },
                toolbox: {
                    show: true,
                    orient: 'vertical',
                    left: 'right',
                    top: 'center',
                    feature: {
                        dataView: {readOnly: false},
                        restore: {},
                        saveAsImage: {}
                    }
                },
                series: [
                    {
                        name: '合同数',
                        type: 'map',
                        mapType: 'china',
                        roam: false,
                        label: {
                            normal: {
                                show: true
                            },
                            emphasis: {
                                show: true
                            }
                        },
                        data: []
                    },
                    {
                        name: '总金额',
                        type: 'map',
                        mapType: 'china',
                        label: {
                            normal: {
                                show: true
                            },
                            emphasis: {
                                show: true
                            }
                        },
                        data: []
                    }
                ]
            }
        };
        this.constants = {
            titles: {
                category: "各行业",
                product: "各种产品",
                province: "各省份",
                purpose: "各种用途"
            },
            datas: {
                count: "合同量",
                money: "总金额",
                all: "合同量及总金额"
            }
        }
        this.setOption = function ($scope, pic) {
            baseService.query({
                url: baseUrl + '/contract/analysis',
                params: {
                    type: $scope.condition.type,
                    group: $scope.condition.group,
                    data: $scope.condition.data
                }
            }).then(function (data) {
                pic.clear();
                pic.showLoading();
                var option;
                option = self.options[$scope.condition.type + 'Option'];
                option.title.text = self.constants.titles[$scope.condition.group] +
                    self.constants.datas[$scope.condition.data] + "统计";
                if ($scope.condition.type == 'bar') {
                    option.xAxis[0].data = data.categories;
                    option.series[0].name = self.constants.datas[$scope.condition.data];
                } else {
                    option.legend.data = data.categories;
                }
                option.series[0].data = data.data;
                if ($scope.condition.type == 'map')
                    option.series[1].data = data.money;
                pic.hideLoading();
                pic.setOption(option);
            });
        }
    }])
    .controller('analysisCtrl', ['$http', '$scope', 'analysisService', function ($http, $scope, analysisService) {
        var pic = echarts.init(document.getElementById('pic'));
        $scope.condition = {
            type: 'bar',
            group: 'category',
            data: 'count',
            province: false,
            all: true
        };
        analysisService.setOption($scope, pic);
        $scope.changeType = function () {
            if ($scope.condition.type == "map") {
                $scope.condition.province = true;
                $scope.condition.all = false;
                $scope.condition.data = 'count';
                $scope.condition.group = 'province';
            } else {
                $scope.condition.all = true;
                $scope.condition.province = false;
            }
            analysisService.setOption($scope, pic);
        };
        $scope.changeGroup = function () {
            analysisService.setOption($scope, pic);
        };
        $scope.changeData = function () {
            analysisService.setOption($scope, pic);
        };
    }])
;
